<template>
  <el-dialog
    :visible.sync="visible"
    :before-close="beforeClose"
    :close-on-click-modal="false"
    append-to-body
    title="大数据查询"
    class="dialog-sm"
    top="30px"
    width="80%"
  >
    <el-form @keyup.enter.native="getData()" :inline="true">
      <el-form-item>
        <el-input v-model="params.customer_name" clearable placeholder="项目名称查询"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.corp_name" clearable placeholder="负责人查询"></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          @change="getData()"
          :auto-top="1"
          placeholder="地区查询"
          clearable
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
      height="60vh"
      ref="filterTable"
      :empty-text="emptyText"
    >
      <template v-slot:customerName="scope">
        <div
          class="text-primary cursor-pointer project-history"
          :data-history="scope.row.is_history"
        >
          {{ scope.row.customer_name }}
        </div>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <new-project
      :has-refuse="false"
      :visible.sync="loanVisible"
      @success="getData()"
      :customerRelatedId="loanRelatedId"
      title="贷一笔"
    ></new-project>
  </el-dialog>
</template>
<script>
import { mapGetters } from 'vuex'

import SelectAddress from '@/components/select/SelectAddress'
import NewProject from '@/bigbank/views/library/components/BankApprove.vue'

export default {
  components: {
    SelectAddress,
    NewProject,
  },
  props: {
    visible: Boolean,
  },
  data() {
    return {
      loading: false,
      params: {
        index: 1,
        size: 15,
        address_code: '',
        customer_name: '',
        corp_name: '',
      },
      order: {
        create_time: 'desc',
      },
      tableName: 'customer_related',
      tableCount: 0,
      tableData: [],
      tableColumns: [
        {
          attrs: { label: '项目名称', prop: 'customer_name', align: 'center' },
          slot: 'customerName',
        },
        { attrs: { label: '负责人姓名', prop: 'corp_name', align: 'center' } },
        { attrs: { label: '地址', prop: 'address', align: 'center', minWidth: 120 } },
        { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center' } },
        { attrs: { label: '所属行业', prop: 'tbd_key', align: 'center' } },
        { attrs: { label: '受理次数', prop: 'tbd_key', align: 'center' } },
        { attrs: { label: '放款次数', prop: 'tbd_key', align: 'center' } },
        { attrs: { label: '受理机构数', prop: 'tbd_key', align: 'center' } },
        {
          attrs: { label: '操作', align: 'center', minWidth: '100' },
          operations: [
            { content: '贷一笔', icon: 'fa fa-gavel', event: this.handleNewLoan },
            { content: '查信用', icon: 'fa fa-search', event: this.handleCredit },
          ],
        },
      ],
      emptyText: '请输入项目名称或者负责人名称查询',

      // 贷一笔
      loanVisible: false,
      loanRelatedData: {},
      loanRelatedId: '',
    }
  },
  computed: {
    ...mapGetters(['user', 'clientId']),
    queryAddressCode() {
      return this.user.queryAddressCode
    },
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    beforeClose() {
      this.$emit('update:visible', false)
    },
    async getData(index = 1) {
      if (!this.params.customer_name && !this.params.corp_name) {
        this.emptyText = '请输入项目名称或者负责人名称查询'
        return this.$message.warning('请输入项目名称/负责人名称查询')
      }
      this.emptyText = '暂无数据'
      this.params.index = index
      this.loading = true
      const [res] = await this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, this.params, this.order)
      )
      this.loading = false

      const data = (res && res.data) || {}
      this.tableCount = data.totalCount || 0
      this.tableData = (data.data || []).map(this.statusAdapter)
    },
    statusAdapter(item) {
      return item
    },

    // 贷一笔
    handleNewLoan(item = {}) {
      this.loanRelatedId = item.customer_related_id
      this.loanRelatedData = { ...item }
      this.loanVisible = true
    },

    handleCredit(_item) {
      //
    },
  },
}
</script>
<style lang="scss" scoped></style>
